//
//  UIs_Sudoku_Demo2VC.swift
//  APSwift
//
//  Created by jianbo.pei on 2023/10/8.
//

import UIKit
import APExtensions

class UIs_Sudoku_Demo2VC: UIViewController {
    var scrollView: UIScrollView!

    let titles: [String] = [
        "北京",
        "上海",
        "重庆",
        "天津",
        "郑州",
        "西安",
        "合肥",
        "广州",
        "哈尔滨",
        "南京",
        "武汉",
        "长沙",
        "云南",
        "河南",
        "湖南",
        "厦门",
        "苏州",
        "杭州",
        "武汉",
        "南昌",
        "乌鲁木齐",
        "北京2",
        "上海2",
        "重庆2",
        "天津2",
        "郑州2",
        "西安2",
        "合肥2",
        "广州2",
        "哈尔滨2",
        "南京2",
        "武汉2",
        "长沙2",
        "云南2",
        "河南2",
        "湖南2",
        "厦门2",
        "苏州2",
        "杭州2",
        "武汉2",
        "南昌2",
        "乌鲁木齐2"
    ]

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        view.backgroundColor = .white
        
        scrollView = UIScrollView(frame: CGRect(x: 0, y: 150, width: ap_kScreenW, height: ap_kScreenH - 300))
        view.addSubview(scrollView)
        #if DEBUG
        scrollView.layer.masksToBounds = true
        scrollView.layer.cornerRadius = 8
        scrollView.layer.borderColor = UIColor.blue.cgColor
        scrollView.layer.borderWidth = 4
        #endif

        test1()
        
//        test2()

    }
}

extension UIs_Sudoku_Demo2VC {
    fileprivate func test1() {
        Sudoku.layout { config in
            config.itemHeight = 60
            config.itemCount = self.titles.count
            config.horizontalSpacing = 15
            self.scrollView.contentSize = CGSize(width: ap_kScreenW, height: config.calculateHeight())
        } completion: { (frame, index) in
            _ = UILabel().then { make in
                scrollView.addSubview(make)
                make.frame = frame
                make.textAlignment = .center
                make.text = self.titles[vmSafe: index]
                make.textColor(.ap.hex("ff4d4d"))
                make.backgroundColor = UIColor.ap.hex("ff4d4d", alpha: 0.1)
                if index == 4 {
                    make.layer.masksToBounds = true
                    make.layer.borderWidth = 1
                    make.layer.cornerRadius = 4
                    make.layer.borderColor = UIColor.ap.hex("ff4d4d").cgColor
                }
            }
        }
    }
    
    fileprivate func test2() {
        var config = SudokuConfiguration.default
        config.itemHeight = 60
        config.itemCount = self.titles.count
        config.horizontalSpacing = 15
        
        Sudoku.layout(config) { (frame, index) in
            _ = UILabel().then { make in
                scrollView.addSubview(make)
                make.frame = frame
                make.textAlignment = .center
                make.text = self.titles[vmSafe: index]
                make.textColor(.ap.hex("ff4d4d"))
                make.backgroundColor = UIColor.ap.hex("ff4d4d", alpha: 0.1)
                if index == 4 {
                    make.layer.masksToBounds = true
                    make.layer.borderWidth = 1
                    make.layer.cornerRadius = 4
                    make.layer.borderColor = UIColor.ap.hex("ff4d4d").cgColor
                }
            }
        }
        
        scrollView.contentSize = CGSize(width: ap_kScreenW, height: Sudoku.calculateHeight(config))
    }
}

